package com.android.lyricshow.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.until.KEY;
import com.android.lyricshow.until.LogPrint;
import com.android.lyricshow.until.OP;

import android.content.Intent;
import android.media.MediaPlayer;

public class PlayManager extends MediaPlayer {
	
	public static final int NORMAL			= 1007;
	public static final int LOOP_ALL		= 1008;
	public static final int LOOP_ONE		= 1009;
	public static final int SHUFF			= 1010;
	
	private PlayListener mPL;
	
	private List<SongBean> mSongList = new ArrayList<SongBean>();
	
	private int mMode		= NORMAL;
	
	private int mCurIndex	= 0;
	
	private boolean mPause	= false;
	
	public PlayManager(){
		this.setOnCompletionListener(mCompletion);
		this.setOnPreparedListener(mPrepared);
		this.setOnErrorListener(mError);
	}
	
	public void startPlay(){
		synchronized (mSongList) {
			if(mSongList.size() == 0){
				return;
			}
			songPlay(OP.START);
		}
	}
	
	public void clear(){
		synchronized (mSongList){
			
			mMode		= 0;	 	
			mCurIndex	= 0;
			
			mSongList.clear();
		}
	}
	
	public void setPlayList(List<SongBean> songlist){
		synchronized (mSongList){
			clear();
			mSongList = songlist;
		}
		
		LogPrint.d("" + mSongList.size());
	}
	
	public void resume(){
		if(mPause){
			start();
			mPause = false;
		}
	}
	
	public void pause(){
		super.pause();
		mPause = true;
	}
	
	public int getCurPos(){
		if(mPause || isPlaying()){
			return this.getCurrentPosition();
		}
		return 0;
	}
	
	public void setIndex(int index){
		mCurIndex = index;
	}
	
	public void setMode(int mode){
		mMode = mode;
		
		synchronized (mSongList){
			if(mMode == SHUFF){
				
				SongBean sbean = mSongList.get(mCurIndex);
				
				mSongList.remove(mCurIndex);
				Collections.shuffle(mSongList);
				
				mCurIndex = 0;
				mSongList.add(0, sbean);
			}
		}
	}
	
	public void playNext(){
		
		synchronized (mSongList){
			if(mCurIndex >= mSongList.size() - 1){
				return;
			}
			
			mCurIndex++;
			songPlay(OP.NEXT);
		}
	}
	
	public void playPrevious(){
		
		synchronized (mSongList){
			if(mCurIndex >= mSongList.size() - 1){
				return;
			}
			
			mCurIndex++;
			songPlay(OP.NEXT);
		}
	}
	
	private void songPlay(int code){
		
		SongBean sbean = mSongList.get(mCurIndex);
		
		try {
			reset();
			setDataSource(sbean.getPath());
			prepareAsync();
			
			LogPrint.d("prepareAsync + " + System.currentTimeMillis());
		} catch (Exception e) {
			
			LogPrint.e(e);	
			mPL.Error(PlayListener.ERROR_READ_FILE);
			return;
		}
		
		Intent it = new Intent();
		it.putExtra(KEY.SONG_BEAN, sbean);
		mPL.playInfo(code, it);
		
		mPause = false;
	}
	
	public void addToPlayList(ArrayList<SongBean> list){
		synchronized (mSongList){
			if(mSongList.size() == 0){
				mSongList.addAll(list);
			}else{
				mSongList.addAll(mCurIndex + 1, list);
			}
		}
	}
	
	public void addToPlayList(SongBean sbean){
		synchronized (mSongList){
			if(mSongList.size() == 0){
				mSongList.add(mCurIndex, sbean);
			}else{
				mSongList.add(mCurIndex + 1, sbean);
			}
		}
	}
	
	public void notifyCurPlay(){
		
		int code = OP.START;
		
		Intent it = new Intent();
		if(mSongList.size() != 0 ){
			
			it.putExtra(KEY.SONG_BEAN, mSongList.get(mCurIndex));
			it.putExtra(KEY.IS_PAUSE, mPause);
			it.putExtra(KEY.CUR_POS, getCurPos());
		}else{
			code = OP.NO_PLAYLIST;
		}

		mPL.playInfo(code, it);
	}
	
	public List<SongBean> getPlayList(){
		return mSongList;
	}
	
	public void setPlayListener(PlayListener el){
		mPL = el;
	}
	
	private OnCompletionListener mCompletion = new OnCompletionListener(){

		@Override
		public void onCompletion(MediaPlayer mp) {
			LogPrint.d("Play completed, and play next.");
			playNext();	
		}		
	};
	
	private OnPreparedListener mPrepared = new OnPreparedListener(){

		@Override
		public void onPrepared(MediaPlayer mp) {
			mp.start();
		}
	};
	
	private MediaPlayer.OnErrorListener mError = new MediaPlayer.OnErrorListener(){

		@Override
		public boolean onError(MediaPlayer mp, int what, int extra) {
			mPL.Error(what);
			return true;
		}
	};
}
